home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Q: mem allocation in function
- Date: Sat, 30 Mar 96 21:27:25 GMT
- Organization: none
- Message-ID: <828221245snz@genesis.demon.co.uk>
- References: <4je0qv$heq@salomon.zfe.siemens.de>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4je0qv$heq@salomon.zfe.siemens.de>
- rainer@nil.zfe.siemens.de "Rainer Wartha" writes:
-
- >
- >Hello,
- >
- >is it conforming to ANSI (allowed) to allocate in
- >a Function Memory for a Pointer (defined external)
-
- Do you mean assign the return value of malloc to a local pointer variable
- in your function?
-
- >and use it after returning from the function
- >(memory not freed, free shortly before exit)
-
- malloc'd memory is valid until it is free'd. There is no problem in returning
- a pointer to such memory from a function. What you can't sensibly do is
- return the address of a local variable in a function e.g.
-
- RTYPE foo(void)
-
- {
- char *ptr = malloc(100);
-
- /* Here you can */
-
- return ptr;
-
- /* But you can't */
-
- return &ptr;
-
- }
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-